#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/5 15:32
# @Author : 闫峰
# @File    : TemuApi.py
import time

import requests

from common.setting import ensure_path_sep
from third_services.temu.sign import SignatureHelper
from utils.mysqlUtils.db_pool import Database


class TemuApi:

    def __init__(self, source_id):
        app_key, access_token, cert_id_access_key_id = self.get_auth(source_id)
        self.cert_id_access_key_id = cert_id_access_key_id

        self.common_data = {
            # "access_token": "rzmnpnugdufkaedajgpvdplg1qay5py9dstfqzw6cnnv4fxo3ttgjtz1",
            # "app_key": "1554c0c854451793629cae6a892bae2d",
            "access_token": access_token,
            "app_key": app_key,
            "data_type": "JSON",
            "timestamp": int(time.time())
            # "timestamp": 7924509800
        }
        self.url = "https://openapi.kuajingmaihuo.com/openapi/router"

    def bg_goods_quantity_update(self, request_dict):
        map_type = ".".join(self.bg_goods_quantity_update.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res

    def bg_goods_quantity_get(self, request_dict):
        map_type = ".".join(self.bg_goods_quantity_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res

    def bg_goods_list_get(self, request_dict):
        """

        {

    "page": 1,
    "pageSize": 50,
    "productSkcIds": [
    ]
}
        """
        map_type = ".".join(self.bg_goods_list_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res

    def get_auth(self, source_id):
        data_db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"),
                           "HWY-ZH-GZ-RDS-JavaCommon")
        sql = f"""
        select dev_id_seller_id,merchant_token,cert_id_access_key_id
        from `zhcxkj-center-data`.t_temu_ordersource_authorization
        where source_id = %s
        """
        res = data_db.fetch_all_tuple(sql, (source_id,))
        return res[0]


    def bg_goods_parentspec_get(self):
        map_type = ".".join(self.bg_goods_parentspec_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res



    def bg_logistics_template_get(self):
        map_type = ".".join(self.bg_logistics_template_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res


    def bg_goods_warehouse_list_get(self):
        """
        {
          "type": "bg.goods.warehouse.list.get",
          "timestamp": 1713363745,
          "app_key": "72bc9e4143e960b2134e1cdf22fec651",
          "data_type": "JSON",
          "access_token": "9e5ectqbpbtwxice49baks4lpgbef7rk3v53ckqqhqobufl4ah2jn7fo",
          "siteIdList": [
            100,
            101,
            102,
            103
          ],
          "sign": "60C1984140E9DA6C8B9E4BEAC3FF53E2"
        }

        """
        map_type = ".".join(self.bg_goods_warehouse_list_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res


    def bg_goods_cats_get(self,request_dict):
        """
        {
            "access_token": " token ",
            "app_key": "app_key",
            "data_type": "JSON",
            "parentCatId": 0,
            "showHidden": "false",
            "sign": "DA45F3B65475F8D0106B994FCC7239A8",
            "siteId": 1,
            "timestamp": 1663760440,
            "type": "bg.goods.cats.get"
        }
        """
        map_type = ".".join(self.bg_goods_cats_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res


    def bg_goods_attrs_get(self,request_dict):
        """
        {
               "access_token": " access_token ",
               "app_key": " app_key ",
               "catId": 27144,  # 叶子节点。
               "data_type": "JSON",
               "sign": "49874443565CB441EB3DED56744BA96F",
               "timestamp": 1663760924,
               "type": "bg.goods.attrs.get"
        }
        """
        map_type = ".".join(self.bg_goods_attrs_get.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res

    def bg_product_search(self,request_dict):
        """
        {
            "type": "bg.product.search",
            "timestamp": 1720687614,
            "app_key": "0535a65e0012aaf1199b09b2443bc71c",
            "data_type": "JSON",
            "access_token": "hotbalctap2ubzg0m2cyclkwcrppza3luwvslwafal2yp5ptih4lcedz",
            "productSkuIdList":[2726711814,1885105373,3140432197],
            "mallId":9447356363,
            "pageSize":10,
            "pageNum":1,
            "sign": "89AF58783D0F95F6D7234DA2711FCCAC"
        }
                ● 状态枚举：
        0, "已弃用"
        1, "待平台选品",
        14, "待卖家修改",
        15, "已修改",

        16, "服饰可加色",

        2, "待上传生产资料",

        3, "待寄样",

        4, "寄样中",

        5, "待平台审版",

        6, "审版不合格",

        7, "平台核价中",

        8, "待修改生产资料",

        9, "核价未通过",

        10, "待下首单",

        11, "已下首单",

        12, "已加入站点",

        13, "已下架",

        17, "已终止",
        """
        map_type = ".".join(self.bg_product_search.__name__.split('_'))
        self.common_data.update(request_dict)
        self.common_data.update({"type": map_type})
        sign = SignatureHelper(self.cert_id_access_key_id).get_sign(self.common_data)
        self.common_data.update({"sign": sign})
        res = requests.post(self.url, json=self.common_data)
        return res

if __name__ == '__main__':
    """
    22091	TEMU-Z12001-US-半托管 1433
    22516	TEMU-Z14001-US-半托管 71 
    22743	TEMU-Z12003-US-半托管 1890
    22913	TEMU-Z12004-US-半托管 287
    """
    # request_dict = {        "page": 1,
    # "pageSize": 50,
    #                         "productSkcIds": [1200340184
    #                         ]
    # }
    # request_dict = {"parentCatId": 20,"showHidden": "false","siteId": 1}
    # request_dict =         {
    #            "catId": 5302 # 叶子节点。
    #     }
    # TemuApi(access_token,app_key).bg_goods_list_get(request_dict)
    # TemuApi(22913).bg_goods_parentspec_get()
    request_dict = {'productSkcId': 2114957424}
    request_dict = {

    'skuStockChangeList': [
        {
            "productSkuId": 3383125383,
            "stockDiff": 4,
            "warehouseId": 'WH-01704945387670329'
        }
    ]
}
    TemuApi(23284).bg_goods_quantity_update(request_dict)
